package com.itheima.mythread.Demo5;

public class SellTicket implements Runnable {
    private int tickets = 30;
    private Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (obj) {
                if (tickets < 0) {
                    System.out.println(Thread.currentThread().getName()+"票卖完了!!!");
                    break;
                } else {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println(Thread.currentThread().getName() + "正在售票,还剩" + tickets + "张票");
                    tickets--;
                }
            }
        }
    }
}
